package com.swifty.toptoastbar;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

/**
 * Created by Swifty on 2016/2/6.
 */
public class FloatWindowService {

    public static final String SERVICE_WINDOWS_PARENT = "service_parent";

    static void onCommand(Context context, Intent intent) {
        context.getUITaskDispatcher().asyncDispatch(() -> {
            long delay = intent.getLongParam("delay", 0);
            long time = intent.getLongParam("time", 0);
            int bgColor = intent.getIntParam("backgroundColor", 0);
            int textColor = intent.getIntParam("textColor", 0);
            Toast.Position position = intent.getSerializableParam("position");
            String message = intent.getStringParam("message");
            final WindowManager windowManager = WindowManager.getInstance();
            WindowManager.LayoutConfig layoutParams = new WindowManager.LayoutConfig();
            layoutParams.type = WindowManager.LayoutConfig.MOD_TOAST;

            layoutParams.flags = WindowManager.LayoutConfig.MARK_OUTSIDE_TOUCH | WindowManager.LayoutConfig.MARK_TOUCHABLE_IMPOSSIBLE;
            layoutParams.width = ComponentContainer.LayoutConfig.MATCH_PARENT;
            layoutParams.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;

            Toast toast = (Toast) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_view_message, null, false);
            toast.onFinishInflate();
            toast.setVisibility(Component.INVISIBLE);
            StackLayout frameLayout = new StackLayout(context);
            frameLayout.setTag(SERVICE_WINDOWS_PARENT);
            frameLayout.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT));

            toast.setText(message)
                    .setPosition(position)
                    .setTime(time);
            if (bgColor != 0) {
                toast.setBackground(bgColor);
            }
            if (textColor != 0) {
                toast.setTextColor(textColor);
            }

            toast.show(delay);
            StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT, StackLayout.LayoutConfig.MATCH_CONTENT);
            layoutConfig.alignment = position == Toast.Position.BOTTOM ? LayoutAlignment.BOTTOM : LayoutAlignment.TOP;
            frameLayout.addComponent(toast, layoutConfig);
            final Window currWindow = windowManager.addComponent(frameLayout, context, WindowManager.LayoutConfig.MOD_TOAST);
            currWindow.setLayoutConfig(layoutParams);
            currWindow.setTransparent(true);
            toast.addToastListener(new ToastListenerAdapter() {
                @Override
                public void afterToastDismiss(Toast toast) {
                    if (currWindow != null) {
                        windowManager.destroyWindow(currWindow);
                    }
                }
            });
        });
    }
}